import {encodeTimestamp} from './date-tools';
const RE_PHONE = /^1\d{10}$/;

export default class Form {
  
  constructor(attrs, valideSetter, defaultValide=false) {
    this.attrs = attrs;
    this.values = {};
    this.valide = defaultValide;
    this._valideSetter = valideSetter;
  }

  setValue(name, value){
    this.values[name] = value;
    this.autoCheckValide();
  }
  
  setValues(values){
    Object.assign(this.values, values);
    this.autoCheckValide();
  }
  
  autoCheckValide(){
    let valide = this._validateRequires();
    if (valide !== this.valide){
      this.valide = valide;
      this._valideSetter(this.valide);
    }
  }
  
  getValues(errorCallback){
    let values = {};
    for(let name in this.attrs){
      let attr = this.attrs[name];
      let value = this.values[name];
      if (value !== undefined && value !== null){
        let {error, value} = this.validate(attr, this.values[name], attr.label||name)
        if (error){
          if (errorCallback){
            errorCallback(error);
          }
          return
        } else {
          values[name] = value;
        }
      }
    }
    return values;
  }
  
  getValue(name, errorCallback){
    let attr = this.attrs[name];
    let rawValue = this.values[name];
    if (rawValue !== undefined && rawValue !== ''){
      let {error, value} = this.validate(attr, rawValue, attr.label||name);
      if (error){
        errorCallback && errorCallback(error);
      } else {
        return value;
      }
    } else {
      errorCallback && errorCallback((attr.label||name)+'未填写');
    }
  }
  
  _validateRequires(){
    for(let name in this.attrs){
      let attr = this.attrs[name];
      if (attr.required){
        if (this.values[name] === undefined || this.values[name] === ''){
          return false;
        }
      }
    }
    return true;
  }
  
  validate(attr, value, label){
    if (attr.type === 'number' && typeof(value) === 'string'){
      value = parseFloat(value);
      if (isNaN(value)){
        return {error: label+'请输入数字'}
      }
    } else if (attr.type === 'boolean' && typeof(value) !== 'boolean'){
      value = !!value;
    }
    
    if (attr.type === 'date'){
      if (!(value instanceof Date)){
        return {error: label+'请输入日期'}
      } else {
        value = encodeTimestamp(value);
      }
    } else if (attr.type === 'phone'){
      if (typeof(value) !== 'string' || !RE_PHONE.test(value)){
        return {error:'请填写正确的手机号'};
      }
    } else if (attr.type === 'array'){
      if(!Array.isArray(value)){
        return {error: label+'请输入队列'}
      }
    } else if (typeof(value) !== attr.type){
      return {error: label+'输入类型不正确'}
    }
    
    if(typeof(value) === 'number' && attr.max){
      if (value >attr.max){
        return {error: label+'不能大于'+attr.max}
      }
    }
    if(typeof(value) === 'number' && attr.min){
      if (value < attr.min){
        return {error: label+'不能小于'+attr.min}
      }
    }
    if(typeof(value) === 'string' && attr.maxLen){
      if (value.length > attr.maxLen){
        return {error: label+'长度不能大于'+attr.min}
      }
    }
    if(typeof(value) === 'string' && attr.minLen){
      if (value < attr.minLen){
        return {error: label+'长度不能小于'+attr.minLen}
      }
    }
    return {value};
  }
  
}